class Factory < ActiveRecord::Base
  
  validates :name, :link_phone, :link_man, :address, :presence => true
  validates :name, :uniqueness => true
  validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }

  scope :visible, where('status = ?','visible')
    
  def self.being_order_mades    
    (OrderItem::FactoryOrderMade.being_order_mades + InstallItem.being_order_mades).each_with_object([]) do |item,factories|
      if not factories.include?(item.furniture.furniture_style.factory)
        factories << item.furniture.furniture_style.factory
      end
    end
  end
  
  def can_be_hidden?
    (Material.where( :factory_id => id).visible + FurnitureStyle.where( :factory_id => id).visible).empty?
  end
  
  def visible!
    update_attribute :status, 'visible'
  end

  def hidden!
    update_attribute :status, 'hidden'
  end

  def status
    ActiveSupport::StringInquirer.new(self[:status])
  end  
end
